package com.lwb.edustatistics.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lwb.commonutils.Row;
import com.lwb.edustatistics.client.UcenterMemberClient;
import com.lwb.edustatistics.entity.StatisticsDaily;
import com.lwb.edustatistics.mapper.StatisticsDailyMapper;
import com.lwb.edustatistics.service.StatisticsDailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-12-14
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private UcenterMemberClient ucenterMemberClient;

    @Override
    public void registerCount(String day) {

        //先删除之前的记录
        QueryWrapper<StatisticsDaily> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("date_calculated",day);
        baseMapper.delete(queryWrapper);

        //查询数据
        Row row = ucenterMemberClient.countRegister(day);
        Integer count = (Integer) row.getData().get("count");

        //插入数据
        StatisticsDaily statisticsDaily = new StatisticsDaily();
        statisticsDaily.setRegisterNum(count);
        statisticsDaily.setDateCalculated(day);
        statisticsDaily.setLoginNum(null);
        statisticsDaily.setCourseNum(null);
        statisticsDaily.setVideoViewNum(null);

        baseMapper.updateById(statisticsDaily);
    }

    @Override
    public Map<String, Object> showDate(String type, String begin, String end) {
        QueryWrapper<StatisticsDaily> queryWrapper = new QueryWrapper<>();
        queryWrapper.between("date_calculated",begin,end);
        List<StatisticsDaily> sdList = baseMapper.selectList(queryWrapper);


        Map<String, Object> map = new HashMap<>();
        List<Integer> dataList = new ArrayList<Integer>();
        List<String> dateList = new ArrayList<String>();

        for (int i=0;i<sdList.size();i++){
            StatisticsDaily statisticsDaily = sdList.get(i);
            dateList.add(statisticsDaily.getDateCalculated());
            switch (type) {
                case "register_num":
                    dataList.add(statisticsDaily.getRegisterNum());
                    break;
                case "login_num":
                    dataList.add(statisticsDaily.getLoginNum());
                    break;
                case "video_view_num":
                    dataList.add(statisticsDaily.getVideoViewNum());
                    break;
                case "course_num":
                    dataList.add(statisticsDaily.getCourseNum());
                    break;
                default:
                    break;
            }
        }

        map.put("dataList",dataList);
        map.put("dateList",dateList);
        return map;
    }
}
